#include "Class/Manager/Scene.hpp"

namespace		Manager
{
  bool			Scene::Run()
  {
	this->_Graphics.Open();
	if (!this->_Graphics.IsOpened())
	  return (false);

	float		time;

	while (this->_Graphics.IsOpened())
	  {
		time = this->_Graphics.GetFrameTime();

		this->_Input.Update(time);
		if (!this->_UpdateScenes(time))
		  break ;
		this->_DrawScenes();
		this->_Graphics.Display();
	  }

	this->_Graphics.Close();

	return (true);
  }

  bool			Scene::_UpdateScenes(float time)
  {
	Scenes::reverse_iterator	rit = this->_Scenes.rbegin(), rite = this->_Scenes.rend();

	for ( ; rit != rite ; ++rit)
	  {
		if ((*rit)->GetState() == ::Scene::AScene::BlockAll)
		  break ;
		else if ((*rit)->GetState() == ::Scene::AScene::Delete)
		  {
			this->_Scenes.erase(rit.base() - 1);
			break ;
		  }
		else if ((*rit)->GetState() == ::Scene::AScene::Delete)
		  {
			this->_Scenes.clear();
			break ;
		  }

		if ((*rit)->Active())
		  (*rit)->Update(time);
	  }
	return (this->_Scenes.size() != 0);
  }

  void			Scene::_DrawScenes()
  {
	Scenes::reverse_iterator	rit = this->_Scenes.rbegin(), rite = this->_Scenes.rend();

	for ( ; rit != rite ; ++rit)
	  {
		if ((*rit)->Visible())
		  (*rit)->Draw();
	  }
  }
}
